package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService {
    /**
     * 根据前端请求的数据,查询频道~
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAdchannel(ChannelDTO dto) {
        //参数校验
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否有频道名称进行模糊查询
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName,dto.getName());
        }
        //添加排序
        wrapper.orderByAsc(AdChannel::getOrd);
        //校验分页条件
        dto.checkParam();
        //根据传参设置分页
        Page page = new Page(dto.getPage(), dto.getSize());
        //调用MyBatisPlus根据分页查询方法
        IPage result = this.page(page, wrapper);
        //参数返回
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),page.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    /**
     * 新建频道
     * @param channel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        //校验参数合法性
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"数据不能为空");
        }
        //判断频道名称长度
        if (channel.getName().length() > 10 ||channel.getName() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"请重新设定名称");
        }
        //判断该名称是否已存在
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdChannel::getName, channel.getName());
        if (this.count(wrapper) > 0 ){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }
        //添加数据
        channel.setCreatedTime(new Date());
        this.save(channel);
        //返回响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 频道修改
     * @param channel
     * @return
     */
    @Override
    public ResponseResult updateForId(AdChannel channel) {
        //校验参数合法性
        if (channel == null || channel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"请输入修改的参数");
        }
        //根据传入的id查询要修改的对象
        AdChannel cl = getById(channel.getId());
        //判断数据是否存在
        if (cl == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"数据不存在");
        }
        //判断名称是否修改?传进来的名称参数不为空,且与用id从数据库中查询出来的名称不一致
        if (StringUtils.isNotBlank(channel.getName()) && !channel.getName().equals(cl.getName())){
            //判断修改后的名称是否存在
            LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery()
                    .eq(AdChannel::getName, channel.getName());

            if (this.count(wrapper)> 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已存在");
            }
        }
        //修改
        updateById(channel);
        //返回响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据Id删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //校验数据
        if (id == null || id <= 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id获取频道并判断状态是否为true,则不能删除
        AdChannel channel = this.getById(id);
        //判断数据是否存在或状态是否为启动
        if (channel == null || channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"启动状态下无法删除");
        }
        //根据id删除频道
        this.removeById(id);
        //返回响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
